package com.stox.client.ui.javafx.widget;

import javafx.event.EventHandler;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.stage.WindowEvent;

import com.stox.shared.core.model.intf.Initializable;

public abstract class AbstractInitializableContextMenu extends ContextMenu implements Initializable, EventHandler<WindowEvent> {

	public AbstractInitializableContextMenu() {
		setOnShowing(this);
	}
	
	@Override
	public void handle(WindowEvent event) {
		initialize();
	}
	
	private boolean initialized = false;
	@Override
	public void initialize() {
		if(!initialized){
			initialized = true;
			for(MenuItem menuItem : getItems()){
				if(menuItem instanceof Initializable){
					Initializable initializable = (Initializable)menuItem;
					initializable.initialize();
				}
			}
		}
	}

}
